<template>
    <div>
        <Row>
          <Col :span="span1">
            <iview-table
                    ref="table"
                    :span1="20"
                    :span2="4"
                    :baseURL="queryModel.baseURL"
                    :menuId="dataModel.menuId"
                    :columns="dataModel.columns"
                    :order="dataModel.order"
                    :pageNumber="pageModel.pageNumber"
                    :pageSize="pageModel.pageSize"
                    :defaultFilters="queryModel.defaultFilters"
                    v-on="{changeTotalSize:changeTotalSize,events:events,dblclick:dblclick,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page
                    :totalSize="pageModel.totalSize"
                    :pageNumber="pageModel.pageNumber"
                    :pageSize="pageModel.pageSize"
                    :pageSizeOpts="pageModel.pageSizeOpts"
                    v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
          </Col>
          <Col :span="span2">
            <Card :bordered="false" :dis-hover="true" style="height: auto">
              <p>
                <Tag v-if="homeId !== ''" type="dot" color="blue">{{homeName}}</Tag>
                <Button type="info" @click="back">返回</Button>
              </p>
              <Card :dis-hover="true" :bordered="false">
                <p slot="title">
                  基本方案
                </p>
                <Tabs>
                  <TabPane label="处置项目">
                    <treat-plan-home-project-list ref="l2" :homeId="homeId" :planType="1"></treat-plan-home-project-list>
                  </TabPane>
                  <Button @click="addProject" type="primary" size="default" slot="extra">添加项目</Button>
                </Tabs>
                <br>
                <Tabs>
                  <TabPane label="处方产品">
                    <treat-plan-home-product-list ref="l1" :homeId="homeId" :planType="1"></treat-plan-home-product-list>
                  </TabPane>
                  <Button @click="addProduct" type="primary" size="default" slot="extra">添加产品</Button>
                </Tabs>
              </Card>
              <Card :dis-hover="true" :bordered="false">
                <p slot="title">
                  可选方案
                </p>
                <Tabs>
                  <TabPane label="处置项目">
                    <treat-plan-home-project-list ref="l4" :homeId="homeId" :planType="2"></treat-plan-home-project-list>
                  </TabPane>
                </Tabs>
                <br>
                <Tabs>
                  <TabPane label="处方产品">
                    <treat-plan-home-product-list ref="l3" :homeId="homeId" :planType="2"></treat-plan-home-product-list>
                  </TabPane>
                </Tabs>
              </Card>
            </Card>
          </Col>
        </Row>
        <Modal v-model="showAddProduct" :closable='false' :mask-closable=true :width="1000">
          <h3 slot="header" style="color:#2D8CF0">请选择产品</h3>
          <Tabs type="card">
            <TabPane label="请选择">
              <product-list ref="list1" v-on="{select:selectProduct}"></product-list>
            </TabPane>
            <TabPane :label="label1">
              <Table border :columns="columns1" :data="data1"></Table>
            </TabPane>
            <Button type="primary" @click="doAddProduct" style="height: 30px" slot="extra">提交</Button>
          </Tabs>
          <div slot="footer">
          </div>
        </Modal>
        <Modal v-model="showAddProject" :closable='false' :mask-closable=true :width="1000">
          <h3 slot="header" style="color:#2D8CF0">请选择项目</h3>
          <Tabs type="card">
            <TabPane label="请选择">
              <project-list ref="list2" v-on="{select:selectProject}"></project-list>
            </TabPane>
            <TabPane :label="label2">
              <Table border :columns="columns2" :data="data2"></Table>
            </TabPane>
            <Button type="primary" @click="doAddProject" style="height: 30px" slot="extra">提交</Button>
          </Tabs>
          <div slot="footer">
          </div>
        </Modal>
        <Modal v-model="showModify" :closable='false' :mask-closable=true :width="400">
          <h3 slot="header" style="color:#2D8CF0">请确认价格</h3>
          <Row>
            <Col span="22" offset="1">
              <Form :label-width="60">
                <FormItem label="总价格：">
                  <InputNumber :min="1" v-model="totalPrice" style="width: 100%"></InputNumber>
                </FormItem>
              </Form>
            </Col>
          </Row>
          <div slot="footer">
            <Button type="primary" @click="doModify" style="height: 30px" slot="extra">确认</Button>
          </div>
        </Modal>
    </div>
</template>

<script>
    import treatPlanHomeProductList from '@/view/business/medical/list/TreatPlanHomeProductList.vue';
    import treatPlanHomeProjectList from '@/view/business/medical/list/TreatPlanHomeProjectList.vue';
    import productList from "@/view/basic/info/list/ProductList.vue";
    import projectList from "@/view/basic/info/list/ProjectList.vue";
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    import Table from "../../../common/table/Table";
    export default {
        components: {
            Table,
            iviewTable,
            iviewPage,
            treatPlanHomeProductList,
            treatPlanHomeProjectList,
            productList,
            projectList
        },
        data () {
            return {
                label1: (h) => {
                    return h('div', [
                        h('span', '已选择'),
                        h('Badge', {
                            props: {
                                count: this.data1.length
                            }
                        })
                    ])
                },
                label2: (h) => {
                    return h('div', [
                        h('span', '已选择'),
                        h('Badge', {
                            props: {
                                count: this.data2.length
                            }
                        })
                    ])
                },
                span1: '24',
                span2: '0',
                homeId: '',
                homeName: '',
                memberId: '',
                totalPrice: '',
                selectRow: {},
                selectRows: [],
                showAddProduct: false,
                showAddProject: false,
                showModify: false,
                baseURL: '/user/treat/plan/home',
                productURL: '/user/treat/plan/home/product',
                projectURL: '/user/treat/plan/home/project',
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treat/plan/home',
                    defaultFilters: [
                        {'field': 'complaint_id', 'value1': '-1', 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 680,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '添加时间',
                            key: 'creationTime',
                            render: (h, params) => {
                                return h('span', this.$moment(params.row.creationTime).format('YYYY-MM-DD'))
                            }
                        },
                        {
                            title: '病房名称',
                            key: 'homeName'
                        },
                        {
                            title: '治疗周期',
                            key: 'treatCycle',
                            render: (h, params) => {
                                let temp = params.row.treatCycle
                                if (temp === 0) {
                                    temp = '不限时间'
                                } else {
                                    temp += '年'
                                }
                                return h('span', temp)
                            }
                        },
                        {
                            title: '单价',
                            key: 'price'
                        },
                        {
                            title: '购买数量',
                            key: 'quantity'
                        },
                        {
                            title: '购买价格',
                            key: 'totalPrice'
                        },
                        {
                            title: '收费状态',
                            key: 'chargeStatus',
                            render: (h, params) => {
                                let temp = params.row.chargeStatus
                                if (temp === 0) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'yellow'
                                            },
                                            style: {
                                            }
                                        }, '待交费')
                                    ]);
                                } else if (temp === 1) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'green'
                                            },
                                            style: {
                                            }
                                        }, '已交费')
                                    ]);
                                }
                            }
                        },
                        {
                            title: '目前状态',
                            key: 'status',
                            render: (h, params) => {
                                let temp = params.row.status
                                if (temp === 0) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'green'
                                            },
                                            style: {
                                            }
                                        }, '正常')
                                    ]);
                                } else if (temp === 1) {
                                    return h('div', [
                                        h('Tag', {
                                            props: {
                                                type: 'border',
                                                color: 'red'
                                            },
                                            style: {
                                            }
                                        }, '已冻结')
                                    ]);
                                }
                            }
                        },
                        {
                            title: '操作',
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.view(params.row, params.index)
                                            }
                                        }
                                    }, '查看'),
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.delete(params.row)
                                            }
                                        }
                                    }, '删除')
                                ]);
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [5, 10, 20, 50, 100]
                },
                data1: [],
                columns1: [
                    {
                        title: '大类',
                        key: 'bigClassName',
                        width: 150
                    },
                    {
                        title: '小类',
                        key: 'smallClassName',
                        width: 150
                    },
                    {
                        title: '产品名称',
                        key: 'name',

                    },
                    {
                        title: '产品价格',
                        key: 'price',
                        width: 150

                    },
                    {
                        title: '数量',
                        key: 'quantity',
                        width: 150,
                        render: (h, params) => {
                            var vm = this
                            return h('InputNumber',{
                                props: {
                                    min: 1,
                                    value: params.row.quantity
                                },
                                style: {
                                    width: '100%'
                                },
                                on: {
                                    'on-change' (value) {
                                        params.row.quantity = value
                                        vm.data1[params.index] = params.row
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: '操作',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove1(params.index)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                data2: [],
                columns2: [
                    {
                        title: '项目类别',
                        key: 'projectTypeName',
                        width: 150
                    },
                    {
                        title: '项目名称',
                        key: 'name',

                    },
                    {
                        title: '项目价格',
                        key: 'price',
                        width: 150
                    },
                    {
                        title: '数量',
                        key: 'quantity',
                        width: 150,
                        render: (h, params) => {
                            var vm = this
                            return h('InputNumber',{
                                props: {
                                    min: 1,
                                    value: params.row.quantity
                                },
                                style: {
                                    width: '100%'
                                },
                                on: {
                                    'on-change' (value) {
                                        params.row.quantity = value
                                        vm.data2[params.index] = params.row
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: '操作',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove2(params.index)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ]
            };
        },
        methods: {
            events (item) {
                if (item.path === 'freeze') {
                    this.freeze()
                } else if (item.path === 'use') {
                    this.use()
                } else if (item.path === 'renewal') {
                    this.renewal()
                } else if (item.path === 'modify') {
                    this.modify()
                }
            },
            freeze () {
                if (this.selectRows.length === 1) {
                    let url = this.baseURL + '/freeze/' + this.selectRows[0].id
                    this.$ajax.post(url).then(response => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '冻结成功')
                            this.refresh()
                        } else {
                            this.$bus.alert('warning', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            use () {
                if (this.selectRows.length === 1) {
                    let url = this.baseURL + '/active/' + this.selectRows[0].id
                    this.$ajax.post(url).then(response => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '激活成功')
                            this.refresh()
                        } else {
                            this.$bus.alert('warning', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete (row) {
                if (row.chargeStatus === 1) {
                    this.$bus.alert('warning', '不符合删除条件')
                    return
                }
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认删除当前记录?</p>',
                    onOk: () => {
                        this.doDelete(row.id)
                    }
                })
            },
            doDelete (id) {
                let url = this.baseURL + '/deleteById/' + id
                this.$ajax.post(url).then(response => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '删除成功')
                        this.homeId = ''
                        this.homeName = ''
                        this.refresh()
                        this.refreshDetails()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            renewal () {
                this.$emit('renewal')
            },
            modify () {
                if (this.selectRows.length === 1) {
                    this.selectRow = this.selectRows[0]
                    this.totalPrice = ''
                    this.showModify = true
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            doModify () {
                let url = '/user/treat/plan/home/modify'
                this.$ajax.post(url, {
                    id: this.selectRow.id,
                    totalPrice: this.totalPrice
                }).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '修改成功')
                        this.showModify = false
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            dblclick (row, index) {
                this.homeId = row.id
                this.homeName = row.homeName
                this.memberId = row.memberId
            },
            addProduct () {
                if (this.homeId !== '') {
                    this.data1 = []
                    this.$refs.list1.refresh()
                    this.showAddProduct = true
                } else {
                    this.$bus.alert('warning', '请双击选择左侧某个病房')
                }
            },
            selectProduct (row) {
                row.patientId = this.$store.state.app.patientId
                row.complaintId = this.$store.state.app.complaintId
                row.memberId = this.memberId
                row.homeId = this.homeId
                row.homeName = this.homeName
                row.productId = row.id
                row.productName = row.name
                row.quantity = 1
                this.data1.push(row)
            },
            doAddProduct () {
                if (this.data1.length > 0) {
                    let url = this.productURL + '/add'
                    this.$ajax.post(url, this.data1).then(r => {
                        let {code, msg} = r.data
                        if (code === 0) {
                            this.$bus.alert('success', '添加成功')
                            this.showAddProduct = false
                            this.refreshDetails()
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '没有选择任何产品')
                }
            },
            remove1 (index) {
                this.data1.splice(index, 1)
            },
            view (row, index) {
                this.homeId = row.id
                this.homeName = row.homeName
                this.memberId = row.memberId
                this.span1 = '0'
                this.span2 = '24'
            },
            back () {
                this.span1 = '24'
                this.span2 = '0'
            },
            addProject () {
                if (this.homeId !== '') {
                    this.data2 = []
                    this.$refs.list2.refresh()
                    this.showAddProject = true
                } else {
                    this.$bus.alert('warning', '请双击选择左侧某个病房')
                }
            },
            selectProject (row) {
                row.patientId = this.$store.state.app.patientId
                row.complaintId = this.$store.state.app.complaintId
                row.memberId = this.memberId
                row.homeId = this.homeId
                row.homeName = this.homeName
                row.projectId = row.id
                row.projectName = row.name
                row.quantity = 1
                this.data2.push(row)
            },
            doAddProject () {
                if (this.data2.length > 0) {
                    let url = this.projectURL + '/add'
                    this.$ajax.post(url, this.data2).then(r => {
                        let {code, msg} = r.data
                        if (code === 0) {
                            this.$bus.alert('success', '添加成功')
                            this.showAddProject = false
                            this.refreshDetails()
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '没有选择任何项目')
                }
            },
            remove2 (index) {
                this.data2.splice(index, 1)
            },
            refresh () {
                this.queryModel.defaultFilters[0] = {'field': 'complaint_id', 'value1': this.$store.state.app.complaintId, 'operate': '=='}
                this.$refs.table.refresh()
            },
            refreshDetails () {
                this.$refs.l1.refresh()
                this.$refs.l2.refresh()
                this.$refs.l3.refresh()
                this.$refs.l4.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            }
        }
    }
</script>

<style>
    .sag {
        width: 100%;
        height: 1000px;
        border-radius: 4px;
        /*border:1px solid #333;*/
        box-shadow:inset 0 0 5px 5px #ccc;
        padding: 10px;
    }
</style>
